package com.timmy.customkeyboardtest;

import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import java.security.InvalidKeyException;
import java.util.List;
import java.util.logging.Level;

/**
 * Created by timmy on 2020/4/15.
 */
public class CustomKeyboard extends KeyboardView implements KeyboardView.OnKeyboardActionListener {

    private Animation mAnimationSlideInBottom;

    private final NameA mNameA = new NameA(this, new Keyboard(getContext(), R.xml.dl_keyboard));

    private NameA mNameE = this.mNameA;

    private List<Keyboard.Key> f = this.mNameE.mKeyboard.getKeys();

    private boolean g = false;

    private SingleKeyCallback h;

    private boolean i = false;

    private boolean j = false;

    private OnHideFinishListener k;
    private Animation            mAnimationSlideOutBottom;

    public CustomKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public CustomKeyboard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setOnKeyboardActionListener(this);
        setPreviewEnabled(true);
        setHapticFeedbackEnabled(true);
        setSoundEffectsEnabled(true);
        setEnabled(false);

        mAnimationSlideInBottom = AnimationUtils.loadAnimation(getContext(), R.anim.dl_anim_slide_in_bottom);
        mAnimationSlideInBottom.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                setEnabled(true);
                setFocusable(true);
                setFocusableInTouchMode(true);
                requestFocus();

            }


            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


        mAnimationSlideOutBottom = AnimationUtils.loadAnimation(getContext(), R.anim.dl_anim_slide_out_bottom);
        mAnimationSlideOutBottom.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationEnd(Animation animation) {
                setEnabled(false);
                setFocusable(false);
                setFocusableInTouchMode(false);
                clearFocus();

                getLayoutParams().height = 0;
                requestLayout();

                if (h != null) {
                    h.onHide();
                }

                if (k != null) {
                    k.hideFinish();
                }
            }

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    private void e() {
        setShifted(!isShifted());

        for (Keyboard.Key key : f) {
            if (key.label != null && key.label.length() == 1) {
                char c = key.label.charAt(0);
                if (c >= 'a' && c <= 'z') {
                    key.label = String.valueOf((char) (c - 32));
                } else if (c >= 'A' && c <= 'Z') {
                    key.label = String.valueOf((char) (c + 32));
                }

                switch (c) {
                    default:
                        switch (c) {
                            default:
                                switch (c) {
                                    default:
                                        continue;
                                    case '~':
                                        key.label = String.valueOf('`');
                                        continue;
                                    case '}':
                                        key.label = String.valueOf(']');
                                        continue;
                                    case '|':
                                        key.label = String.valueOf('\\');
                                        continue;
                                    case '{':
                                        break;
                                }
                                key.label = String.valueOf('[');
                                continue;
                            case '`':
                                key.label = String.valueOf('~');
                                continue;
                            case '_':
                                key.label = String.valueOf('-');
                                continue;
                            case '^':
                                key.label = String.valueOf('6');
                                continue;
                            case ']':
                                key.label = String.valueOf('}');
                                continue;
                            case '\\':
                                key.label = String.valueOf('|');
                                continue;
                            case '[':
                                break;
                        }
                        key.label = String.valueOf('{');
                        continue;
                    case '@':
                        key.label = String.valueOf('2');
                        continue;
                    case '?':
                        key.label = String.valueOf('/');
                        continue;
                    case '>':
                        key.label = String.valueOf('.');
                        continue;
                    case '=':
                        key.label = String.valueOf('+');
                        continue;
                    case '<':
                        key.label = String.valueOf(',');
                        continue;
                    case ';':
                        key.label = String.valueOf(':');
                        continue;
                    case ':':
                        key.label = String.valueOf(';');
                        continue;
                    case '9':
                        key.label = String.valueOf('(');
                        continue;
                    case '8':
                        key.label = String.valueOf('*');
                        continue;
                    case '7':
                        key.label = String.valueOf('&');
                        continue;
                    case '6':
                        key.label = String.valueOf('^');
                        continue;
                    case '5':
                        key.label = String.valueOf('%');
                        continue;
                    case '4':
                        key.label = String.valueOf('$');
                        continue;
                    case '3':
                        key.label = String.valueOf('#');
                        continue;
                    case '2':
                        key.label = String.valueOf('@');
                        continue;
                    case '1':
                        key.label = String.valueOf('!');
                        continue;
                    case '0':
                        key.label = String.valueOf(')');
                        continue;
                    case '/':
                        key.label = String.valueOf('?');
                        continue;
                    case '.':
                        key.label = String.valueOf('>');
                        continue;
                    case '-':
                        key.label = String.valueOf('_');
                        continue;
                    case ',':
                        key.label = String.valueOf('<');
                        continue;
                    case '+':
                        key.label = String.valueOf('=');
                        continue;
                    case '*':
                        key.label = String.valueOf('8');
                        continue;
                    case ')':
                        key.label = String.valueOf('0');
                        continue;
                    case '(':
                        key.label = String.valueOf('9');
                        continue;
                    case '\'':
                        key.label = String.valueOf('"');
                        continue;
                    case '&':
                        key.label = String.valueOf('7');
                        continue;
                    case '%':
                        key.label = String.valueOf('5');
                        continue;
                    case '$':
                        key.label = String.valueOf('4');
                        continue;
                    case '#':
                        key.label = String.valueOf('3');
                        continue;
                    case '"':
                        key.label = String.valueOf('\'');
                        continue;
                    case '!':
                        break;
                }
                key.label = String.valueOf('1');
            }
        }
        invalidateAllKeys();
    }

    public String a(int paramInt) {
        if (paramInt == 59) {
            return getResources().getString(R.string.dl_keylabel_shift);
        }

        String str = "";
        for (Keyboard.Key key : f) {
            if (paramInt == key.codes[0]) {
                if (paramInt == getResources().getInteger(R.integer.dl_keycode_backspace)) {
                    return getResources().getString(R.string.dl_keylabel_back);
                }

                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(key.label);
                stringBuilder.append("");
                str = stringBuilder.toString();
            }
        }
        return str;
    }

    @Override
    public void onPress(int primaryCode) {

    }

    @Override
    public void onRelease(int primaryCode) {

    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {

    }

    @Override
    public void onText(CharSequence text) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }


    private class NameA {
        Keyboard mKeyboard;
        public int b;

        public NameA(CustomKeyboard keyboard, Keyboard paramKeyboard) {
            mKeyboard = paramKeyboard;
            this.b = b;
        }
    }

    public static interface SingleKeyCallback {
        void onHide();

        void onKey(boolean param1Boolean, String param1String, int param1Int1, int param1Int2);

        void switchToGameKeyboard();
    }

    public static interface OnHideFinishListener {
        void hideFinish();
    }

}
